unit Unit1;
{$mode Delphi}{$H+}
interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
  ComCtrls, fphttpclient, SyncObjs;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Memo1: TMemo;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Label1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    time: integer;
  public
    procedure UpdateMemo(const Message: string);
  end;


  THttpRequestThread = class(TThread)
    Url: string;
    HTTPClient: TFPHTTPClient;
    Response: string;
    procedure UpdateUI;
  protected
    procedure Execute; override;
  public
    constructor Create(const Url: string);
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }
procedure TForm1.UpdateMemo(const Message: string);
begin
  Memo1.Lines.Add(Message);
end;


procedure TForm1.Label1Click(Sender: TObject);
begin
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  t1: string;
  HttpRequestThread: THttpRequestThread;
begin
  t1 := Edit1.Text;
  HttpRequestThread:= THttpRequestThread.Create(t1);
  HttpRequestThread.start;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  time := 0;
  Memo1.Text := '';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Timer1.Enabled <> True then
  begin
    Button1.Caption := '停止';
    Timer1.Enabled := True;
  end
  else
  begin

    Button1.Caption := '开始';
    Timer1.Enabled := False;
  end;
end;

constructor  THttpRequestThread.Create(Const Url: String);
begin
  inherited Create(True);
  FreeOnTerminate := True;
  Self.Url:=Url;
end;


procedure THttpRequestThread.UpdateUI;
begin
  Form1.UpdateMemo('[' + FormatDateTime('yyyy-mm-dd hh:nn:ss', Now) + '] ' + Response);
end;

procedure THttpRequestThread.Execute;
var exec:TThreadMethod;
begin
     HTTPClient := TFPHTTPClient.Create(nil);
     exec := UpdateUI;
     try
       HTTPClient.ConnectTimeout := 1000;
       Response := HTTPClient.Get(Url);
       Synchronize(nil,exec);
     except
       on E: Exception do
        begin
        Response := E.Message;
        Synchronize(nil,exec);
        end;
     end;
     HTTPClient.Free;
end;


end.
